import { RuleDao } from '@/dao/ruleDao'
import { TaskDao } from '@/dao/taskDao'
import { pageFns } from '../utils/pageHelper'
import { UsageScope } from '@/entities/Constant'

async function startPost(article, page) {
    try {
        let whereDict = {
            enable: 1,
            usageScope: UsageScope.Publisher
        }
        let tasks = await TaskDao.fetchAll(whereDict)
        for (let task of tasks) {
            await pageFns.overridePermissions(page, task.homeUrl)
            let rule = await RuleDao.fetchById(task.ruleId)
            let postFns = require('./' + rule.module)
            let res = await postFns.goPost(page, task, article, rule)
            if (res.err) {
                throw new Error(res.err)
            }
        }
        return { err: null }
    } catch (err) {
        return { err }
    }
}
export { startPost }